package cn.kgm.makeGodV2.emotion;

import lombok.Builder;
import lombok.Data;

/**
 * 瞬时情绪 & 长时间情绪
 * 三维情感向量：愉悦度(Valence)、唤醒度(Arousal)、控制度(Dominance)
 */
@Data
@Builder
public class EmotionalVector {

    private final double valence;      // 愉悦度 [0.0, 100.0]
    private final double arousal;      // 唤醒度 [0.0, 100.0]
    private final double dominance;    // 控制度 [0.0, 100.0]

    public EmotionalVector(double valence, double arousal, double dominance) {
        this.valence = clamp(valence, 0.0, 100.0);
        this.arousal = clamp(arousal, 0.0, 100.0);
        this.dominance = clamp(dominance, 0.0, 100.0);
    }

    // 创建预定义情感
    public static EmotionalVector createJoy() {
        return new EmotionalVector(85.0, 70.0, 65.0);
    }

    public static EmotionalVector createAnger() {
        return new EmotionalVector(20.0, 90.0, 80.0);
    }

    public static EmotionalVector createFear() {
        return new EmotionalVector(25.0, 85.0, 20.0);
    }

    public static EmotionalVector createSadness() {
        return new EmotionalVector(15.0, 25.0, 30.0);
    }

    public static EmotionalVector createCalm() {
        return new EmotionalVector(70.0, 30.0, 75.0);
    }

    public static EmotionalVector createNeutral() {
        return new EmotionalVector(50.0, 50.0, 50.0);
    }

    public static EmotionalVector createAnxiety() {
        return new EmotionalVector(35.0, 75.0, 25.0);
    }

    public static EmotionalVector createContent() {
        return new EmotionalVector(75.0, 40.0, 60.0);
    }

    private double clamp(double value, double min, double max) {
        return Math.max(min, Math.min(max, value));
    }

    // 向量运算
    public EmotionalVector add(EmotionalVector other) {
        return new EmotionalVector(
                this.valence + other.valence,
                this.arousal + other.arousal,
                this.dominance + other.dominance
        );
    }

    public EmotionalVector subtract(EmotionalVector other) {
        return new EmotionalVector(
                this.valence - other.valence,
                this.arousal - other.arousal,
                this.dominance - other.dominance
        );
    }

    public EmotionalVector multiply(double factor) {
        return new EmotionalVector(
                this.valence * factor,
                this.arousal * factor,
                this.dominance * factor
        );
    }

    public EmotionalVector divide(double divisor) {
        return new EmotionalVector(
                this.valence / divisor,
                this.arousal / divisor,
                this.dominance / divisor
        );
    }

    public double distanceTo(EmotionalVector other) {
        double vDiff = this.valence - other.valence;
        double aDiff = this.arousal - other.arousal;
        double dDiff = this.dominance - other.dominance;
        return Math.sqrt(vDiff * vDiff + aDiff * aDiff + dDiff * dDiff);
    }

    public double magnitude() {
        return Math.sqrt(valence * valence + arousal * arousal + dominance * dominance);
    }
}
